<?php
class IndexController extends CommonController
{
	public function Index(){
		// 载入菜单缓存
		$menu_list	= $this->_menu_list();
		$menu_num	= $menu_list[0]['num'];
		include $this->display('index.html');
	}

	public function Main(){
		include $this->display('main.html');
	}
	// 菜单缓存
	private function _menu_list(){
		$menu_list = $this->cache->read('menu_list');
		if ($menu_list) {
			return $menu_list;
		} else {
			$list     = $this->db->table("@_menu")->order("sort ASC")->findAll();
			$menu_num = count($list);
			foreach ($list as $key => $v) {
				$list[$key]['num']	= $menu_num;
				$list[$key]['node']	= $this->db->table("@_role_node")->order("sort ASC, id ASC")->findAll();
			}
			$this->cache->write($list, 'menu_list');
			return $list;
		}
	}
	// 系统配置
	public function System(){
		$list = $this->db->table("@_system_info")->order("id ASC")->findAll();
		$lang_item	= $this->cache->read('lang_item');
		foreach ($list as $key => $vv) {
			$list[$key]['lang_str']		= $lang_item[$vv['lang_id']];
		}
		include $this->display('system.html');
	}
	// 添加配置
	public function Addsystem(){
		if ( subMit('data') ){
			$data				= getGpc('data', 'string', 'P');
			$data['lang_id'] 	= intval($data['lang_id']);
			if (empty($data['name'])) {
				$this->msg('请填写名称。');
			}
			if (empty($data['lang_id'])) {
				$this->msg('请选择语言');
			}
			$vo = $this->db->table("@_system_info")->where("lang_id={$data['lang_id']}")->find();
			if (!empty($vo)) {
				$this->msg('同一种语言只能添加一个配置');
			}
			$this->db->table("@_system_info")->add($data);
			$result['status']	= 1;
			$result['msg']		= '添加成功';
			echo json_encode($result);
			exit();
		}
		$lang_item	= $this->cache->read('lang_item');
		ob_start();
		include $this->display('system.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	public function Editsystem(){
		$id 		= getGpc('id', 'integer', 'G');
		if (empty($id)) {
			$this->msg('参数错误。');
		}
		if ( isset($_POST) && $_POST ){
			$data				= getGpc('data', 'string', 'P');
			$data['lang_id'] 	= intval($data['lang_id']);
			if (empty($data['name'])) {
				$this->msg('请填写名称。');
			}
			if (empty($data['lang_id'])) {
				$this->msg('请选择语言');
			}
			$vo = $this->db->table("@_system_info")->where("id<>$id AND lang_id={$data['lang_id']}")->find();
			if (!empty($vo)) {
				$this->msg('同一种语言只能添加一个配置');
			}
			$this->db->table("@_system_info")->where("id=$id")->update($data);
			$result['status']	= 1;
			$result['msg']		= '修改成功';
			echo json_encode($result);
			exit();
		}
		$vo = $this->db->table("@_system_info")->where("id=$id")->find();
		if (empty($vo)) {
			$this->msg('不存在此配置');
		}
		$lang_item	= $this->cache->read('lang_item');
		ob_start();
		include $this->display('system.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}
}
?>